package figure8.Network;

public class Message {
	public static enum Type {
		CLIENT_JOINED,
		CLIENT_LEFT,
		CLIENT_CHAT, 
		
		YOU_ARE,
		
		SERVER_SHUTDOWN,
		
		PROJECT_NEW,
		
		TRACK_ADD,
		TRACK_REMOVE,
		TRACK_UPDATE, 
		TRACK_PIECE;
	}
	
	public Type type;
	public String data;
	
	public Message(String encoded) {
		String[] parts = encoded.split(" ", 2);
		
		this.type = Type.values()[Integer.parseInt(parts[0])];
		this.data = parts[1];
	}
	
	public Message(Type type, String data) {
		this.type = type;
		this.data = data;
	}
	
	public String encode() {
		return String.format("%s %s", this.type.ordinal(), this.data);
	}
	
	public String toString() {
		if (this.type == Type.TRACK_PIECE) {
			return "TRACK PIECE of length " + this.data.length();
		} else {
			return this.encode();
		}
	}
}
